package com.example.sale.controller;

import com.example.sale.entity.SaleChance;
import com.example.sale.entity.SalePlan;
import com.example.sale.service.PlanService;
import com.example.sale.service.SaleService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/")
public class PlanController {

    @Resource(name = "PlanServiceImpl")
    private PlanService planService;

    @Resource(name = "SaleServiceImpl")
    private SaleService saleService;

    @RequestMapping("/makePlan")
    public String makePlan(String id, HttpServletRequest request) {

        //需关联查询 或者单独查询也可  SalePlan 的 cus_id 对应 SaleChance的 id
        //此处最好用 两条简单查询.关联查询 一对多 导致前台页面 多次遍历
        /* 此参数 id 为 sale_chance 的主键id  即 sales_plan 的 cus_id*/

        SaleChance saleChance = saleService.queryChanceById(id);
        List planList = planService.queryByCusId(id);

        request.setAttribute("saleChance", saleChance);
        request.setAttribute("planList", planList);
        return "sales/plan/makePlan";
    }

    @RequestMapping("/doPlan")
    public String doPlan(String id, HttpServletRequest request) {

        //需关联查询 或者单独查询也可  SalePlan 的 cus_id 对应 SaleChance的 id
        //此处最好用 两条简单查询.关联查询 一对多 导致前台页面 多次遍历
        /* 此参数 id 为 sale_chance 的主键id  即 sales_plan 的 cus_id*/

        SaleChance saleChance = saleService.queryChanceById(id);
        List planList = planService.queryByCusId(id);

        request.setAttribute("saleChance", saleChance);
        request.setAttribute("planList", planList);
        return "sales/plan/doPlan";
    }


    @RequestMapping("/delPlan")
    public String delPlan(String id, HttpServletRequest request) {

        //此id 为sales_plan的 主键id
        System.out.println(id);
        SalePlan sp = planService.queryById(id);
        String cus_id = sp.getCus_id();
        planService.deleteInfo(id);

        return "redirect:makePlan?id=" + cus_id;
    }


    /* 保存 salePlan */
    @RequestMapping("/savePlan")
    public String savePlan(SalePlan salePlan, HttpServletRequest request) {

        planService.savePlan(salePlan);
        request.setAttribute("msg", "保存成功!");
        return "redirect:makePlan?id=" + salePlan.getCus_id();
    }


    /* 保存 salePlan */
    @RequestMapping("/updPlan")
    public String updPlan(SalePlan salePlan, @RequestParam  String type,
                          HttpServletRequest request) {

        String idenfy = request.getParameter("type");
        planService.updateInfo(salePlan);
        SalePlan sp = planService.queryById(salePlan.getId());
        String cus_id = sp.getCus_id();
        request.setAttribute("msg", "保存成功!");

        String result = "";

        //返回不同页面
        if (!StringUtils.isEmpty(idenfy)) {
            if (idenfy.equals("makePlan")) {
                result = "redirect:makePlan?id=" + cus_id ;
            } else if (idenfy.equals("doPlan")) {
                result ="redirect:doPlan?id=" + cus_id;
            } else if(idenfy.equals("success")){
                result ="redirect:queryOppo";
            }
        }

        return result;
    }

}
